package fir.diagram.part;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;

import fir.FirPackage;
import fir.Funcionalidade;
import fir.Informacao;
import fir.Regra;
import fir.diagram.edit.parts.FIREditPart;
import fir.diagram.edit.parts.FuncionalidadeEditPart;
import fir.diagram.edit.parts.InformacaoEditPart;
import fir.diagram.edit.parts.LinkInformacaoRegraEditPart;
import fir.diagram.edit.parts.LinkRegraFuncionalidadeEditPart;
import fir.diagram.edit.parts.LinkRegraInformacaoEditPart;
import fir.diagram.edit.parts.RegraEditPart;
import fir.diagram.providers.FirElementTypes;

/**
 * @generated
 */
public class FirDiagramUpdater {

	/**
	 * @generated
	 */
	public static List getSemanticChildren(View view) {
		switch (FirVisualIDRegistry.getVisualID(view)) {
		case FIREditPart.VISUAL_ID:
			return getFIR_1000SemanticChildren(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getFIR_1000SemanticChildren(View view) {
		if (!view.isSetElement()) {
			return Collections.EMPTY_LIST;
		}
		fir.FIR modelElement = (fir.FIR) view.getElement();
		List result = new LinkedList();
		for (Iterator it = modelElement.getInformacoes().iterator(); it
				.hasNext();) {
			Informacao childElement = (Informacao) it.next();
			int visualID = FirVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == InformacaoEditPart.VISUAL_ID) {
				result.add(new FirNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		for (Iterator it = modelElement.getFuncionalidades().iterator(); it
				.hasNext();) {
			Funcionalidade childElement = (Funcionalidade) it.next();
			int visualID = FirVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == FuncionalidadeEditPart.VISUAL_ID) {
				result.add(new FirNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		for (Iterator it = modelElement.getRegras().iterator(); it.hasNext();) {
			Regra childElement = (Regra) it.next();
			int visualID = FirVisualIDRegistry.getNodeVisualID(view,
					childElement);
			if (visualID == RegraEditPart.VISUAL_ID) {
				result.add(new FirNodeDescriptor(childElement, visualID));
				continue;
			}
		}
		return result;
	}

	/**
	 * @generated
	 */
	public static List getContainedLinks(View view) {
		switch (FirVisualIDRegistry.getVisualID(view)) {
		case FIREditPart.VISUAL_ID:
			return getFIR_1000ContainedLinks(view);
		case InformacaoEditPart.VISUAL_ID:
			return getInformacao_2001ContainedLinks(view);
		case FuncionalidadeEditPart.VISUAL_ID:
			return getFuncionalidade_2002ContainedLinks(view);
		case RegraEditPart.VISUAL_ID:
			return getRegra_2003ContainedLinks(view);
		case LinkRegraFuncionalidadeEditPart.VISUAL_ID:
			return getLinkRegraFuncionalidade_4001ContainedLinks(view);
		case LinkInformacaoRegraEditPart.VISUAL_ID:
			return getLinkInformacaoRegra_4002ContainedLinks(view);
		case LinkRegraInformacaoEditPart.VISUAL_ID:
			return getLinkRegraInformacao_4003ContainedLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getIncomingLinks(View view) {
		switch (FirVisualIDRegistry.getVisualID(view)) {
		case InformacaoEditPart.VISUAL_ID:
			return getInformacao_2001IncomingLinks(view);
		case FuncionalidadeEditPart.VISUAL_ID:
			return getFuncionalidade_2002IncomingLinks(view);
		case RegraEditPart.VISUAL_ID:
			return getRegra_2003IncomingLinks(view);
		case LinkRegraFuncionalidadeEditPart.VISUAL_ID:
			return getLinkRegraFuncionalidade_4001IncomingLinks(view);
		case LinkInformacaoRegraEditPart.VISUAL_ID:
			return getLinkInformacaoRegra_4002IncomingLinks(view);
		case LinkRegraInformacaoEditPart.VISUAL_ID:
			return getLinkRegraInformacao_4003IncomingLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getOutgoingLinks(View view) {
		switch (FirVisualIDRegistry.getVisualID(view)) {
		case InformacaoEditPart.VISUAL_ID:
			return getInformacao_2001OutgoingLinks(view);
		case FuncionalidadeEditPart.VISUAL_ID:
			return getFuncionalidade_2002OutgoingLinks(view);
		case RegraEditPart.VISUAL_ID:
			return getRegra_2003OutgoingLinks(view);
		case LinkRegraFuncionalidadeEditPart.VISUAL_ID:
			return getLinkRegraFuncionalidade_4001OutgoingLinks(view);
		case LinkInformacaoRegraEditPart.VISUAL_ID:
			return getLinkInformacaoRegra_4002OutgoingLinks(view);
		case LinkRegraInformacaoEditPart.VISUAL_ID:
			return getLinkRegraInformacao_4003OutgoingLinks(view);
		}
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getFIR_1000ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getInformacao_2001ContainedLinks(View view) {
		Informacao modelElement = (Informacao) view.getElement();
		List result = new LinkedList();
		result
				.addAll(getContainedTypeModelFacetLinks_linkInformacaoRegra_4002(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getFuncionalidade_2002ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getRegra_2003ContainedLinks(View view) {
		Regra modelElement = (Regra) view.getElement();
		List result = new LinkedList();
		result
				.addAll(getContainedTypeModelFacetLinks_linkRegraFuncionalidade_4001(modelElement));
		result
				.addAll(getContainedTypeModelFacetLinks_linkRegraInformacao_4003(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getLinkRegraFuncionalidade_4001ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLinkInformacaoRegra_4002ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLinkRegraInformacao_4003ContainedLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getInformacao_2001IncomingLinks(View view) {
		Informacao modelElement = (Informacao) view.getElement();
		Map crossReferences = EcoreUtil.CrossReferencer.find(view.eResource()
				.getResourceSet().getResources());
		List result = new LinkedList();
		result.addAll(getIncomingTypeModelFacetLinks_linkRegraInformacao_4003(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getFuncionalidade_2002IncomingLinks(View view) {
		Funcionalidade modelElement = (Funcionalidade) view.getElement();
		Map crossReferences = EcoreUtil.CrossReferencer.find(view.eResource()
				.getResourceSet().getResources());
		List result = new LinkedList();
		result
				.addAll(getIncomingTypeModelFacetLinks_linkRegraFuncionalidade_4001(
						modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getRegra_2003IncomingLinks(View view) {
		Regra modelElement = (Regra) view.getElement();
		Map crossReferences = EcoreUtil.CrossReferencer.find(view.eResource()
				.getResourceSet().getResources());
		List result = new LinkedList();
		result.addAll(getIncomingTypeModelFacetLinks_linkInformacaoRegra_4002(
				modelElement, crossReferences));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getLinkRegraFuncionalidade_4001IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLinkInformacaoRegra_4002IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLinkRegraInformacao_4003IncomingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getInformacao_2001OutgoingLinks(View view) {
		Informacao modelElement = (Informacao) view.getElement();
		List result = new LinkedList();
		result
				.addAll(getOutgoingTypeModelFacetLinks_linkInformacaoRegra_4002(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getFuncionalidade_2002OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getRegra_2003OutgoingLinks(View view) {
		Regra modelElement = (Regra) view.getElement();
		List result = new LinkedList();
		result
				.addAll(getOutgoingTypeModelFacetLinks_linkRegraFuncionalidade_4001(modelElement));
		result
				.addAll(getOutgoingTypeModelFacetLinks_linkRegraInformacao_4003(modelElement));
		return result;
	}

	/**
	 * @generated
	 */
	public static List getLinkRegraFuncionalidade_4001OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLinkInformacaoRegra_4002OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	public static List getLinkRegraInformacao_4003OutgoingLinks(View view) {
		return Collections.EMPTY_LIST;
	}

	/**
	 * @generated
	 */
	private static Collection getContainedTypeModelFacetLinks_linkRegraFuncionalidade_4001(
			Regra container) {
		Collection result = new LinkedList();
		fir.linkRegraFuncionalidade link = container.getFuncionalidade();
		if (LinkRegraFuncionalidadeEditPart.VISUAL_ID != FirVisualIDRegistry
				.getLinkWithClassVisualID(link)) {
			return result;
		}
		Funcionalidade dst = link.getTarget();
		Regra src = link.getSource();
		result.add(new FirLinkDescriptor(src, dst, link,
				FirElementTypes.LinkRegraFuncionalidade_4001,
				LinkRegraFuncionalidadeEditPart.VISUAL_ID));
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getContainedTypeModelFacetLinks_linkInformacaoRegra_4002(
			Informacao container) {
		Collection result = new LinkedList();
		for (Iterator links = container.getRegrasConsumidoras().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof fir.linkInformacaoRegra) {
				continue;
			}
			fir.linkInformacaoRegra link = (fir.linkInformacaoRegra) linkObject;
			if (LinkInformacaoRegraEditPart.VISUAL_ID != FirVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Regra dst = link.getTarget();
			Informacao src = link.getSource();
			result.add(new FirLinkDescriptor(src, dst, link,
					FirElementTypes.LinkInformacaoRegra_4002,
					LinkInformacaoRegraEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getContainedTypeModelFacetLinks_linkRegraInformacao_4003(
			Regra container) {
		Collection result = new LinkedList();
		fir.linkRegraInformacao link = container.getInformacaoProduzida();
		if (LinkRegraInformacaoEditPart.VISUAL_ID != FirVisualIDRegistry
				.getLinkWithClassVisualID(link)) {
			return result;
		}
		Informacao dst = link.getTarget();
		Regra src = link.getSource();
		result.add(new FirLinkDescriptor(src, dst, link,
				FirElementTypes.LinkRegraInformacao_4003,
				LinkRegraInformacaoEditPart.VISUAL_ID));
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getIncomingTypeModelFacetLinks_linkRegraFuncionalidade_4001(
			Funcionalidade target, Map crossReferences) {
		Collection result = new LinkedList();
		Collection settings = (Collection) crossReferences.get(target);
		for (Iterator it = settings.iterator(); it.hasNext();) {
			EStructuralFeature.Setting setting = (EStructuralFeature.Setting) it
					.next();
			if (setting.getEStructuralFeature() != FirPackage.eINSTANCE
					.getlinkRegraFuncionalidade_Target()
					|| false == setting.getEObject() instanceof fir.linkRegraFuncionalidade) {
				continue;
			}
			fir.linkRegraFuncionalidade link = (fir.linkRegraFuncionalidade) setting
					.getEObject();
			if (LinkRegraFuncionalidadeEditPart.VISUAL_ID != FirVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Regra src = link.getSource();
			result.add(new FirLinkDescriptor(src, target, link,
					FirElementTypes.LinkRegraFuncionalidade_4001,
					LinkRegraFuncionalidadeEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getIncomingTypeModelFacetLinks_linkInformacaoRegra_4002(
			Regra target, Map crossReferences) {
		Collection result = new LinkedList();
		Collection settings = (Collection) crossReferences.get(target);
		for (Iterator it = settings.iterator(); it.hasNext();) {
			EStructuralFeature.Setting setting = (EStructuralFeature.Setting) it
					.next();
			if (setting.getEStructuralFeature() != FirPackage.eINSTANCE
					.getlinkInformacaoRegra_Target()
					|| false == setting.getEObject() instanceof fir.linkInformacaoRegra) {
				continue;
			}
			fir.linkInformacaoRegra link = (fir.linkInformacaoRegra) setting
					.getEObject();
			if (LinkInformacaoRegraEditPart.VISUAL_ID != FirVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Informacao src = link.getSource();
			result.add(new FirLinkDescriptor(src, target, link,
					FirElementTypes.LinkInformacaoRegra_4002,
					LinkInformacaoRegraEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getIncomingTypeModelFacetLinks_linkRegraInformacao_4003(
			Informacao target, Map crossReferences) {
		Collection result = new LinkedList();
		Collection settings = (Collection) crossReferences.get(target);
		for (Iterator it = settings.iterator(); it.hasNext();) {
			EStructuralFeature.Setting setting = (EStructuralFeature.Setting) it
					.next();
			if (setting.getEStructuralFeature() != FirPackage.eINSTANCE
					.getlinkRegraInformacao_Target()
					|| false == setting.getEObject() instanceof fir.linkRegraInformacao) {
				continue;
			}
			fir.linkRegraInformacao link = (fir.linkRegraInformacao) setting
					.getEObject();
			if (LinkRegraInformacaoEditPart.VISUAL_ID != FirVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Regra src = link.getSource();
			result.add(new FirLinkDescriptor(src, target, link,
					FirElementTypes.LinkRegraInformacao_4003,
					LinkRegraInformacaoEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getOutgoingTypeModelFacetLinks_linkRegraFuncionalidade_4001(
			Regra source) {
		Regra container = null;
		// Find container element for the link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (EObject element = source; element != null && container == null; element = element
				.eContainer()) {
			if (element instanceof Regra) {
				container = (Regra) element;
			}
		}
		if (container == null) {
			return Collections.EMPTY_LIST;
		}
		Collection result = new LinkedList();
		fir.linkRegraFuncionalidade link = container.getFuncionalidade();
		if (LinkRegraFuncionalidadeEditPart.VISUAL_ID != FirVisualIDRegistry
				.getLinkWithClassVisualID(link)) {
			return result;
		}
		Funcionalidade dst = link.getTarget();
		Regra src = link.getSource();
		if (src != source) {
			return result;
		}
		result.add(new FirLinkDescriptor(src, dst, link,
				FirElementTypes.LinkRegraFuncionalidade_4001,
				LinkRegraFuncionalidadeEditPart.VISUAL_ID));
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getOutgoingTypeModelFacetLinks_linkInformacaoRegra_4002(
			Informacao source) {
		Informacao container = null;
		// Find container element for the link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (EObject element = source; element != null && container == null; element = element
				.eContainer()) {
			if (element instanceof Informacao) {
				container = (Informacao) element;
			}
		}
		if (container == null) {
			return Collections.EMPTY_LIST;
		}
		Collection result = new LinkedList();
		for (Iterator links = container.getRegrasConsumidoras().iterator(); links
				.hasNext();) {
			EObject linkObject = (EObject) links.next();
			if (false == linkObject instanceof fir.linkInformacaoRegra) {
				continue;
			}
			fir.linkInformacaoRegra link = (fir.linkInformacaoRegra) linkObject;
			if (LinkInformacaoRegraEditPart.VISUAL_ID != FirVisualIDRegistry
					.getLinkWithClassVisualID(link)) {
				continue;
			}
			Regra dst = link.getTarget();
			Informacao src = link.getSource();
			if (src != source) {
				continue;
			}
			result.add(new FirLinkDescriptor(src, dst, link,
					FirElementTypes.LinkInformacaoRegra_4002,
					LinkInformacaoRegraEditPart.VISUAL_ID));
		}
		return result;
	}

	/**
	 * @generated
	 */
	private static Collection getOutgoingTypeModelFacetLinks_linkRegraInformacao_4003(
			Regra source) {
		Regra container = null;
		// Find container element for the link.
		// Climb up by containment hierarchy starting from the source
		// and return the first element that is instance of the container class.
		for (EObject element = source; element != null && container == null; element = element
				.eContainer()) {
			if (element instanceof Regra) {
				container = (Regra) element;
			}
		}
		if (container == null) {
			return Collections.EMPTY_LIST;
		}
		Collection result = new LinkedList();
		fir.linkRegraInformacao link = container.getInformacaoProduzida();
		if (LinkRegraInformacaoEditPart.VISUAL_ID != FirVisualIDRegistry
				.getLinkWithClassVisualID(link)) {
			return result;
		}
		Informacao dst = link.getTarget();
		Regra src = link.getSource();
		if (src != source) {
			return result;
		}
		result.add(new FirLinkDescriptor(src, dst, link,
				FirElementTypes.LinkRegraInformacao_4003,
				LinkRegraInformacaoEditPart.VISUAL_ID));
		return result;
	}

}
